/**
 * Initialize your data structure here.
 * @author dingyue.zhf
 * @description 971. 翻转二叉树以匹配先序遍历 https://leetcode-cn.com/problems/flip-binary-tree-to-match-preorder-traversal/
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 *
 * value互不相同
 *
 */
/**
 * @param {TreeNode} root
 * @param {number[]} voyage
 * @return {number[]}
 */
var flipMatchVoyage = function (root, voyage) {
  let i = 0,
    flipList = [];
  if (root.val !== voyage[i]) {
    return [-1];
  }
  const dfs = (node) => {
    i++;
    if (node.left && node.left.val === voyage[i]) {
      dfs(node.left);
    }

    if (node.right && node.right.val === voyage[i]) {
      dfs(node.right);
      // 因为节点不重复
      if (node.left && node.left.val == voyage[i]) {
        flipList.push(node.val);
        dfs(node.left);
      }
    }
  };
  dfs(root);
  return i === voyage.length ? flipList : [-1];
};
